package com.china08.yxyapi.api.manage.tec.teaching.lecture.res;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.util.Assert;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import com.china08.yxyapi.api.YxyApiBaseController;
import com.china08.yxyapi.entity.db.res.YxyResResource;
import com.china08.yxyapi.entity.es.ElasticYxyResResource;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureCatalog;
import com.china08.yxyapi.entity.mg.lecture.YxyLectureRes;
import com.china08.yxyapi.model.YxyApiReqModel;
import com.china08.yxyapi.model.YxyApiRespModel;
import com.china08.yxyapi.repository.db.YxyResResourceRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureCatalogRepository;
import com.china08.yxyapi.repository.mg.lecture.YxyLectureResRepository;
import com.china08.yxyapi.service.yxyresource.YxyResourceService;
import com.china08.yxyapi.util.EsResourcePvUpdateUtil;

import io.swagger.annotations.ApiOperation;

/**
 * 老师 ---备授课--添加备授课资源
 * 
 */
@CrossOrigin
@RestController
public class LectureCrtResController extends YxyApiBaseController {

    @Autowired
    private YxyLectureResRepository yxyLectureResRepository;

    @Autowired
    private YxyLectureCatalogRepository yxyLectureCatalogRepository;

    @Autowired
    private YxyResResourceRepository YxyResResourceRepository;

    @Autowired
    private YxyResourceService ysr;

    @ApiOperation(value = "选入备课资源")
    @RequestMapping(value = "/api_yw/manage/tec/teaching/lecture/res/crt", method = RequestMethod.POST, produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
    public Object process(HttpServletRequest request, HttpServletResponse response, @RequestBody ReqModel reqModel,
            RespModel respModel) {

        String userId = getProfile(request, response).getId();
        String schoolId = getProfile(request, response).getSchoolId();

        Assert.notNull(StringUtils.defaultIfBlank(userId, null), "用户id必须");
        Assert.notNull(StringUtils.defaultIfBlank(schoolId, null), "学校id必须");
        List<String> resourceIdList = reqModel.resourceIdList;

        for (String resourceId : resourceIdList) {
            EsResourcePvUpdateUtil.updateEsResoure(resourceId + "_1");
            // ### 查找资源
            ElasticYxyResResource elasticYxyResResource = ysr.findById(resourceId);
            Assert.notNull(elasticYxyResResource, "资源不存在，请刷新页面");
            // ###
            // 判断是否选入
            Integer countCheck = yxyLectureResRepository
                    .countBySchoolIdAndUserIdAndBookcontentsIdAndResourceIdAndIsDeleteNot(schoolId, userId,
                            reqModel.bookcontentsId, resourceId, 1);
            Assert.isTrue(countCheck == 0, "资源“" + elasticYxyResResource.getTitle() + "”已选入该目录");
            YxyLectureCatalog yxyLectureCatalog = yxyLectureCatalogRepository.findById(reqModel.catalogId);
            YxyLectureRes yxyLectureRes = new YxyLectureRes();
            yxyLectureRes.setUserId(userId);
            yxyLectureRes.setSchoolId(schoolId);
            yxyLectureRes.setPhaseId(yxyLectureCatalog.getPhaseId());
            yxyLectureRes.setPhaseName(yxyLectureCatalog.getPhaseName());
            yxyLectureRes.setSubjectId(yxyLectureCatalog.getSubjectId());
            yxyLectureRes.setSubjectName(yxyLectureCatalog.getSubjectName());
            yxyLectureRes.setVersionId(yxyLectureCatalog.getVersionId());
            yxyLectureRes.setVersionName(yxyLectureCatalog.getVersionName());
            yxyLectureRes.setSemesterId(yxyLectureCatalog.getSemesterId());
            yxyLectureRes.setSemesterName(yxyLectureCatalog.getSemesterName());
            yxyLectureRes.setResName(elasticYxyResResource.getTitle());
            yxyLectureRes.setResUploader(elasticYxyResResource.getUploaderName());
            yxyLectureRes.setResSchool(elasticYxyResResource.getUploaderSchoolName());
            yxyLectureRes.setBookcontentsId(reqModel.bookcontentsId);
            yxyLectureRes.setIsDelete(0);
            yxyLectureRes.setStatus(1);
            YxyResResource findById = YxyResResourceRepository.findById(elasticYxyResResource.getId());
            yxyLectureRes.setFileSize(findById.getFileSize());
            yxyLectureRes.setFormatId(elasticYxyResResource.getFormatId());
            yxyLectureRes.setResourceId(resourceId);
            if (StringUtils.equals(elasticYxyResResource.getTypeId(), "yx14")) {
                yxyLectureRes.setResType(1);
            } else {
                yxyLectureRes.setResType(0);
            }

            yxyLectureRes.setTypeId(elasticYxyResResource.getTypeId());
            yxyLectureResRepository.save(yxyLectureRes);
        }

        respModel = new RespModel();
        respModel.setMessage("备授课目资源存成功");
        return respModel;
    }

    protected static class ReqModel extends YxyApiReqModel {

        public String catalogId;

        /**
         * 目录id
         */
        public String bookcontentsId;

        /**
         * 资源id
         */
        public List<String> resourceIdList;

        @Override
        public void autoCheck() {
            Assert.notNull(StringUtils.defaultIfBlank(catalogId, null), "学段id不能为空");
            Assert.notNull(StringUtils.defaultIfBlank(bookcontentsId, null), "请填写学科");
        }

    }

    protected static class RespModel extends YxyApiRespModel {

    }
}
